<template>
  <div class="listView">
    <div class="activity">
      <div
        v-for="(activity,index) in view.activities"
        :class="['act-btns','act-btns-' + activity.type]"
        :key="index"
        :title="activity.name"
        @click="onAction(activity)"
      >
        {{activity.name}}
      </div>
    </div>
    <el-table
      :data="tableData"
      border
      :height="tableHeight"
      @select="handleSelect"
      @select-all="handelAll"
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55"
      > 
      </el-table-column>
      <template v-for="(column, index) in view.columns">
        <el-table-column
          :key="index"
          :prop="column.id"
          :label="column.name"
          v-if="!column.hiddenColumn"
          :min-width="'120px'"
          >
          <template slot-scope="scope">
            {{scope.row[column.id]}}
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div style="text-align:center;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20,30,40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="row_count">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getViewTemplate,
  getViewData,
  batchRemoveDocuments,
  getAxecuteAddress,
} from "@/api.js"
export default {
  props:[
    "viewId",
    "docId",
    "parentId",
  ],
 data() {
  return {
    view: {},
    tableData:[],
    selectRows: [],
    row_count:0,
    currentPage:1,//当前页1
    pageSize:10,//默认每页10条
    tableHeight:0,
  }
 },
 created() {
  this.toGetViewTemplate()
 },
 mounted() {
  this.$nextTick(() => {
    let height = document.getElementsByClassName('header')[0].clientHeight + document.getElementsByClassName('info')[0].clientHeight
    let stepMainHeight = document.getElementsByClassName('stepMain')[0] ? document.getElementsByClassName('stepMain')[0].clientHeight : 0
    let otherHeight = 40 + 15 + 54 + 48 + 32
    this.tableHeight = document.body.clientHeight - (height + stepMainHeight + otherHeight)
    console.log(this.tableHeight)
  })
   
 },
 methods: {
  handleCurrentChange(val) {
    this.currentPage = val
    this.getData()
  },
  handleSizeChange(val) {
    this.pageSize = val
    this.getData()
  },
  handelAll(selection) {
    console.log(selection)
    this.selectRows = selection
  },
  handleSelect(selection,row) {
    console.log(selection,row)
    this.selectRows = selection
  },
  onAction(act) {
    let type = act.type
    switch(type) {
      case 3: //删除数据
        this.doRemove(act)
        break;
      case 43: //跳转
        this.jumpType(act)
        break;
      default:
        break;
    }

  },
  jumpType(act) {
    debugger
    let obj = {
      parentId:this.parentId
    }
    getAxecuteAddress(
      act.id,
      obj,
      {
        onSucess: res => {
          if(res.data.errcode === 0) {
            let url = res.data.data.content
            let isOpenCurrentSoftwareData = url.search("open?") != -1
            let urlArrayF = url.split("?");
            let urlArray = '';
            if(urlArrayF[1]) {
              urlArray = urlArrayF[1].split("&");
            }else {
              if(url.indexOf('?') == -1) {
                url = url + '?';
              }
            }
            let isExternalLink = false;
            let obj = {};
            if(urlArray && urlArray.length > 0) {
              for(let i=0; i<urlArray.length; i++) {
                let val = urlArray[i].split("=");
                obj[val[0]] = val[1];
              }
            }
            // obj.refreshId = op.id
            if(!isExternalLink) {
              if(isOpenCurrentSoftwareData) {
                let formParams = '';
                for(let key in obj) {
                  if(
                    key != 'name' &&
                    key != 'actionContent' &&
                    key != 'searchData' && 
                    key != 'showtype' && 
                    key != 'realformId' && 
                    key != 'templateForm' && 
                    key != 'urlParams' && 
                    key != 'exparams' &&
                    key != 'runAfterParams' &&
                    key != 'queryString' &&
                    key != 'isOpenDialog' &&
                    key != 'styleParams'
                    ) {
                    if(obj[key]) {
                      formParams += "&" + key + '=' + obj[key];
                    }
                  }
                }
                if(formParams) {
                  obj.formParams = formParams
                }
                obj.name = act.name
              }
            }
            top.obpmOAction('open-eject',obj)
          }
        }
      }
    )
  },
  doRemove(act) {
    let docIds = []
    for(let i = 0; i < this.selectRows.length; i ++) {
      docIds.push(this.selectRows[i].docId)
    }
    if(docIds && docIds.length > 0) {
      this.$confirm('是否需删除'+`${docIds.length}`+'条数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText:'取消',
        type: 'warning'
      }).then(() => {
        this.removeDocuments(act,docIds)
      }).catch(() => {
                
      });
    }else {
      this.$confirm('请选择至少一条数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        
      }).catch(() => {
                
      });
    }
  },
  //删除视图
  removeDocuments(act,docIds) {
    batchRemoveDocuments(docIds, {
      onSucess: (res) => {
        if(res.data.errcode == 0) {
          this.$notify({
            title:'成功',
            message:'',
            type: 'success'
          })
          this.getData()
        }
      }
    });
  },
  toGetViewTemplate() {
    getViewTemplate(
      this.viewId,
      {
        onSucess: response => {
          if(response.data.data) {
            let viewData = response.data.data;
            this.view = viewData
            this.getData()
          }
        }
      }
    )
  },
  getData() {
    let params = {
      parentId: this.docId ? this.docId : '',
      isRelate: '',
      currpage: this.currentPage,
      lines: this.pageSize,
      searchWord: "",
      treedocid: '',
      sortCol: '',
      sortStatus: '',
    }
    getViewData(
      this.viewId,
      params,
      {},
      {
        onSucess: response => {
          debugger
          if(response.data.data) {
            // this.tableData = response.data.data.data
            this.row_count = response.data.data.row_count
            let docs = response.data.data.data
            console.log('docs',docs)
            let tds = [];
            if(docs) {
              docs.forEach((doc) => {
                let rows = {};
                rows.formId = doc.formId
                rows.docId = doc.id
                rows.children = doc.children
                rows.isRunningBack = doc.isRunningBack
                rows.id = new Date().getTime() + Math.random()*1000
                for (var key in doc.items) {
                  let items = doc.items[key]
                  rows[key] = items.value
                }
                tds.push(rows)
              });
            }
            this.tableData = tds
            console.log(this.tableData,'this.tableData')
          }
        }
      }
    )
  }
 },
}
</script>

<style>
@import "../assets/style/listView.scss";
</style>